package com.ruoyi.mine.service.Impl;


import com.ruoyi.backend.propertyConfig.GeoserverConfig;
import com.ruoyi.backend.util.CommmonFileUtils;
import com.ruoyi.backend.util.MinioUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.backendEnum.MineFileException;
import com.ruoyi.mine.dto.MineFileInfoUpdateDTO;
import com.ruoyi.mine.dto.MineFileUploadDTO;
import com.ruoyi.mine.mapper.MineFileMapper;
import com.ruoyi.mine.mapper.MinesMapper;
import com.ruoyi.mine.pojo.Mine;
import com.ruoyi.mine.pojo.MineFile;

import com.ruoyi.mine.service.MineFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class MineFileServiceImpl implements MineFileService {

    @Autowired
    private MineFileMapper mineFileMapper;


    @Autowired
    private GeoserverConfig geoserverConfig;

    @Autowired
    private MinioUtil minioUtil;

    @Value("${minio-bucket.minefile}")
    private String minioBucketName;


    @Override
    public void upload(MineFileUploadDTO mineFileUploadDTO) throws IOException {
        //文件存储：上传到minio
        MultipartFile file = mineFileUploadDTO.getFile();
        // 原始文件名来自前端传递
        //存储在minio当中的文件名：当前系统时分秒+原始文件名组成
        String originalFileName=mineFileUploadDTO.getMineFileName();
        String minioFileName = CommmonFileUtils.getMinioFileName(originalFileName);
        mineFileUploadDTO.setMinioFileName(minioFileName);
        mineFileUploadDTO.setMinioBucketName(minioBucketName);

        //调用封装的Minio工具类来上传矿山文件数据
        minioUtil.uploadFile(minioBucketName, minioFileName, file);

        mineFileMapper.upload(mineFileUploadDTO);
    }

    @Override
    public void delete(Integer id) {
        if (mineFileMapper.getCountId(id) == 0){
            throw new ServiceException(MineFileException.MINEFILE_DATA_NOT_EXIST);
        }
        //先查询要删除的矿山文件数据
        MineFile mineFile = mineFileMapper.findById(id);
        if (mineFile != null) {
            String minioFileName = mineFile.getMinioFileName();
            // 删除 Minio 中指定桶的文件
            boolean removed = minioUtil.removeFile(minioBucketName, minioFileName);
            if (!removed) {
                throw new ServiceException("删除 Minio 文件失败: " + minioFileName);
            }
        }

        mineFileMapper.delete(id);
    }

    @Override
    public void update(MineFileInfoUpdateDTO mineFile) {
        if (mineFileMapper.getCountId(mineFile.getId()) == 0){
            throw new ServiceException(MineFileException.MINEFILE_DATA_NOT_EXIST);
        }
        mineFileMapper.update(mineFile);
    }

    @Override
    public List<MineFile> search(String mineId, String fileType, String fileName) {
//        if (mineFileMapper.search(mineId, fileType, fileName).isEmpty()){
//            throw new ServiceException(MineFileException.MINEFILE_DATA_NOT_FOUND);
//        }
        return mineFileMapper.search(mineId, fileType, fileName);
    }

    @Autowired
    private MinesMapper minesMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public Mine getMine(String mineId) {
        return minesMapper.getMine(mineId);
    }
}
